package filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "CheckLogin",urlPatterns = {"/shop/*"})//urlPatterns代表匹配规则
public class CheckLogin implements Filter {//过滤器
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request=(HttpServletRequest)req;//强制转换
        HttpServletResponse response=(HttpServletResponse)resp;
        HttpSession session= request.getSession();
        Object isLogin=session.getAttribute("isLogin");
        if (isLogin==null){
            System.out.println("失败！");
            response.sendRedirect("../index.html");
        }
        else {
            System.out.println("成功！");
            chain.doFilter(req, resp);//进入下一过滤器
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
